﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.Multi.QrCode;

namespace QRCodeTools.Utils {
    public static class QRCodeHelper {
        /// <summary>
        /// 解析图片中的多个二维码,如果没解析到则返回null,不为null则Length也不为0
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static Result[] ParseQRCode(Bitmap image) {
            QRCodeMultiReader qc = new QRCodeMultiReader();
            LuminanceSource source = new BitmapLuminanceSource(image);
            BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
            IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
            hints.Add(DecodeHintType.CHARACTER_SET, "UTF-8");
            hints.Add(DecodeHintType.TRY_HARDER, true);
            var result = qc.decodeMultiple(binarybitmap, hints);
            if (result == null) return null;
            if (result.Length == 0) return null;
            return result;
        }
    }
}
